package com.dslu.fx.controller;

import com.dslu.fx.dao.EmergencyDepartDao;
import com.dslu.fx.dao.StockBillDao;
import com.dslu.fx.domain.AreaDO;
import com.dslu.fx.domain.EmergencyDO;
import com.dslu.fx.service.DzpService;
import com.dslu.fx.service.EmergencyDepartService;
import com.dslu.fx.utils.CheckEmptyUtil;
import com.dslu.fx.utils.monologfx.MonologFXUtil;
import com.dslu.fx.vo.EmergencyDepartVO;
import com.dslu.fx.vo.EmergencyDzpVO;
import com.dslu.fx.vo.ProdVO;
import de.felixroske.jfxsupport.FXMLController;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import onbon.y2.Y2Exception;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@FXMLController
public class SomeOtherController {

    @FXML
    private TableView<EmergencyDzpVO> tableView2;

    @FXML
    private TextField text_2;

    @FXML
    private TextField text_3;

    @Autowired
    private StockBillDao stockBillDao;

    @Autowired
    private DzpService dzpService;

    @Autowired
    private EmergencyDepartService emergencyDepartService;

    @Autowired
    private EmergencyDepartDao emergencyDepartDao;


    public void reactOnClick2(final Event e) throws Y2Exception {
        String text2 = text_2.getText();//库区名称
        String text3 = text_3.getText();//紧急出库单号

        if(CheckEmptyUtil.isEmpty(text2)){
           // new Alert(Alert.AlertType.INFORMATION, "请输入库区名称", new ButtonType[]{ButtonType.CLOSE}).show();
            MonologFXUtil.alertError("库区名称不能为空，请输入库区名称!");
            return;
        }

        if(CheckEmptyUtil.isEmpty(text3)){
            MonologFXUtil.alertError("紧急出库单号不能为空，请输入单号!");
            return;
        }

        AreaDO areaDO= dzpService.getAreaByName(text2);

        if(areaDO==null){
            MonologFXUtil.alertError("此库区不存在，请确认后再输入");
            return;
        }

        EmergencyDO emergencyDO = emergencyDepartService.getEmergencyByBatchNo(text3);

        if(emergencyDO==null){
            MonologFXUtil.alertError("此紧急出库单号不存在，请确认后再输入");
            return;
        }

        tableView2.getColumns().clear();

        Map map=new HashMap();
        map.put("emergencyId",emergencyDO.getId());
        map.put("areaId",areaDO.getId());

       // List<EmergencyDepartVO> list = dzpService.flushEmergencyDzp(map);

        List<EmergencyDepartVO> list = emergencyDepartDao.getEmergencyDepart(map);

        // List<ProdVO> prodVOs = stockBillDao.getStockByAreaId(40);

        //ObservableList<ProdVO> ls2= FXCollections.observableArrayList();

           ObservableList<EmergencyDzpVO> ls2=FXCollections.observableArrayList();
        list.forEach(w->{
            EmergencyDzpVO emergencyDzpVO=new EmergencyDzpVO();
            emergencyDzpVO.setDepartName(w.getDepartName());
            if(w.getStatus()==0){
                emergencyDzpVO.setStatus("待出库");
            }else if(w.getStatus()==1){
                emergencyDzpVO.setStatus("已出库");
            }else if(w.getStatus()==2){
                emergencyDzpVO.setStatus("已回库");
            }
                ls2.add(emergencyDzpVO);
            });

            tableView2.setItems(ls2);//将集合的值 存储到tableView里

        TableColumn<EmergencyDzpVO, String> departName=new TableColumn<EmergencyDzpVO, String>("库位名称");//创建TableColumn  列名为序号
        TableColumn<EmergencyDzpVO, String> status=new TableColumn<EmergencyDzpVO, String>("状态");

        departName.setCellValueFactory(new PropertyValueFactory<EmergencyDzpVO, String>("departName"));
        status.setCellValueFactory(new PropertyValueFactory<EmergencyDzpVO, String>("status"));

        tableView2.getColumns().add(departName);
        tableView2.getColumns().add(status);

    }
}
